Skip to content

feat: Add BashTool and FileEditorTool subclasses for simplified tool initialization - #88

Merged
xingyaoww merged 5 commits into
mainfrom
openhands/tool-subclasses-issue-86
Sep 3, 2025
Merged

xingyaoww merged 5 commits into
mainfrom
openhands/tool-subclasses-issue-86

Conversation

@xingyaoww

Copy link
Copy Markdown
Contributor

Summary

This PR implements issue #86 by creating BashTool and FileEditorTool subclasses that inherit from Tool, providing a simplified API for tool initialization.

Changes

Core Implementation

  • Created BashTool class in openhands/tools/execute_bash/tool.py

    • Inherits from Tool[ExecuteBashAction, ExecuteBashObservation]
    • Accepts working_dir and optional username parameters
    • Automatically initializes BashExecutor internally
  • Created FileEditorTool class in openhands/tools/str_replace_editor/tool.py

    • Inherits from Tool[StrReplaceEditorAction, StrReplaceEditorObservation]
    • Automatically initializes FileEditorExecutor internally

API Improvements

  • Updated exports in all relevant __init__.py files to include new tool classes
  • Simplified tool initialization from:
    cwd = os.getcwd()
    bash = BashExecutor(working_dir=cwd)
    file_editor = FileEditorExecutor()
    tools: list[Tool] = [
        execute_bash_tool.set_executor(executor=bash),
        str_replace_editor_tool.set_executor(executor=file_editor),
    ]
    To:
    tools = [
        BashTool(working_dir=os.getcwd()),
        FileEditorTool(),
    ]

Examples and Documentation

  • Updated existing examples (hello_world.py, hello_world_with_registry.py) to use simplified pattern
  • Added advanced example (advanced_tools_with_glob.py) demonstrating:
    • Simplified pattern usage
    • Advanced pattern with explicit executor reuse
    • Custom tool creation (GlobTool) with explicit executor definition
  • Updated documentation in .openhands/microagents/repo.md to reflect new architecture

Testing

  • Added comprehensive unit tests for both BashTool and FileEditorTool
  • All tests pass with proper type checking and functionality validation
  • Pre-commit hooks pass with proper formatting and linting

Benefits

  1. Reduced boilerplate: Tool initialization reduced from 5 lines to 2 lines
  2. Better developer experience: Direct instantiation instead of executor setup + set_executor() calls
  3. Backward compatibility: Existing advanced pattern still works for custom use cases
  4. Type safety: Proper generic type annotations for better IDE support

Testing

# Run the new tests
CI=true uv run python -m pytest tests/tools/execute_bash/test_bash_tool.py tests/tools/str_replace_editor/test_file_editor_tool.py -v

# Run pre-commit hooks
uv run pre-commit run --files [modified files]

All tests pass and pre-commit hooks are satisfied.

Fixes #86

@xingyaoww can click here to continue refining the PR

…initialization

- Create BashTool class that inherits from Tool and accepts working_dir/username parameters
- Create FileEditorTool class that inherits from Tool with automatic executor initialization
- Update all __init__.py files to export new tool classes (BashTool, FileEditorTool)
- Simplify examples to use new tool classes instead of manual executor setup
- Add advanced_tools_with_glob.py example showing both simplified and advanced patterns
- Add comprehensive unit tests for both new tool classes
- Update documentation to reflect the new simplified architecture

This change eliminates the need for manual executor instantiation and set_executor() calls,
reducing tool initialization from 5 lines to 2 lines in typical usage.

Fixes #86

Co-authored-by: openhands <openhands@all-hands.dev>
openhands-agent and others added 4 commits September 3, 2025 20:25
- Rewrite GlobExecutor to use bash commands (find and shell globbing) instead of Python's glob module
- Use bash executor for file pattern matching with proper sorting by modification time
- Demonstrate proper executor reuse pattern in advanced example
- Add security_risk parameter to ExecuteBashAction calls

Co-authored-by: openhands <openhands@all-hands.dev>
- Replace GlobTool with GrepTool for content search using regex patterns
- Use bash grep commands instead of file pattern matching
- Support file filtering with include parameter (e.g., '*.py', '*.{ts,tsx}')
- Return matching lines with file paths and line numbers
- Demonstrate proper bash executor reuse pattern
- Update example conversation to search for 'class' keyword in Python files
- Rename file from advanced_tools_with_glob.py to advanced_tools_with_grep.py

Co-authored-by: openhands <openhands@all-hands.dev>
- Move BashTool class from tool.py to execute_bash/definition.py
- Move FileEditorTool class from tool.py to str_replace_editor/definition.py
- Update __init__.py files to import from definition.py instead of tool.py
- Remove separate tool.py files to consolidate code organization
- Add local imports to avoid circular import issues
- All tests still pass and functionality remains unchanged

This follows the cleaner pattern of keeping Tool definitions alongside
their schemas and base tool definitions in a single file.

Co-authored-by: openhands <openhands@all-hands.dev>
@xingyaoww
xingyaoww enabled auto-merge (squash) September 3, 2025 20:38
@xingyaoww
xingyaoww merged commit 8a76a86 into main Sep 3, 2025
@xingyaoww
xingyaoww deleted the openhands/tool-subclasses-issue-86 branch September 3, 2025 20:40
StressTestor pushed a commit to StressTestor/software-agent-sdk that referenced this pull request Jun 1, 2026
…initialization (OpenHands#88)

Co-authored-by: openhands <openhands@all-hands.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inherit from tools to create a subclass for each tools under openhands/tools

2 participants